package Servidor.Ventanas;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import JDBC.JDBCuser;
import Servidor.Clases.User;

public class DatosUsuario extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JLabel passt;
	JLabel nickt;
	JTextField pass;
	JTextField nick;
	
	JButton Guardar;
	JButton eliminar;
	//JButton atras;
	JButton nuevo;
	
	JComboBox lista;
	String[]listaUser=null;
	ArrayList<User>users=null;
	
	User user;
	
	
	public DatosUsuario(){
		
		nickt=new JLabel("Nick: ");
		nickt.setBounds(20, 20, 50, 25);
		nick=new JTextField(50);
		nick.setBounds(80, 20, 100, 25);
		passt=new JLabel("Pass: ");
		passt.setBounds(20, 50, 50, 25);
		pass=new JTextField(50);
		pass.setBounds(80, 50, 100, 25);
		
		Guardar=new JButton("Guardar");
		Guardar.addActionListener(this);
		Guardar.setBounds(20, 90, 80, 30);
		eliminar=new JButton("Eliminar");
		eliminar.addActionListener(this);
		eliminar.setBounds(110, 90, 80, 30);
//		atras= new JButton("Atras");
//		atras.addActionListener(this);
//		atras.setBounds(200, 90, 80, 30);
		nuevo= new JButton("nuevo");
		nuevo.addActionListener(this);
		nuevo.setBounds(290, 90, 80, 30);
	
		
		user=new User();
		JDBCuser bd=new JDBCuser();
		try {			
			users=bd.selectAll();
			bd.close();
			listaUser=new String[users.size()];
			for(int i=0;i<users.size();i++){
				listaUser[i]=users.get(i).getNick();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(listaUser.length!=0){
			lista=new JComboBox(listaUser);
			user=users.get(0);
		}
		else
			lista=new JComboBox();
		lista.addActionListener(this);	
		lista.setBounds(250, 20, 150, 25);
		
		nick.setText(user.getNick());
		pass.setText(user.getPass());
		
		this.setSize(450,200);
		this.add(nick);
		this.add(nickt);
		this.add(pass);
		this.add(passt);
		this.add(Guardar);
		this.add(eliminar);
		//this.add(atras);
		this.add(nuevo);
		this.add(lista);
		
		this.setLayout(null);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}

	private void actualizarLista()
	{
		JDBCuser bd=new JDBCuser();		
		
		try {
			users = bd.selectAll();
			bd.close();
			listaUser= new String[users.size()];
			for(int i=0;i<users.size();i++){
				listaUser[i]=users.get(i).getNick();
			}
			lista.removeAllItems();
			if(listaUser.length!=0){
				for(String s: listaUser)
					lista.addItem(s);
			}
			this.repaint();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object x=arg0.getSource();
		user.setNick(nick.getText());
		user.setPass(pass.getText());
		if(x==Guardar){
			
			try {
				JDBCuser bd= new JDBCuser();
				bd.update(user);
				bd.close();
				actualizarLista();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			if(x==eliminar){
				try {
					JDBCuser bd= new JDBCuser();
					bd.delete(user);
					bd.close();
					actualizarLista();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				if(x==nuevo){
					try {
						JDBCuser bd= new JDBCuser();
						bd.insert(user);
						bd.close();
						actualizarLista();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
//					if(x==atras){
//						this.dispose();
//						ServerWindow sw= new ServerWindow();
//					}else
						if(x==lista){
							try{
								user=users.get(lista.getSelectedIndex());
								nick.setText(user.getNick());
								pass.setText(user.getPass());
							}catch(ArrayIndexOutOfBoundsException ex){
								user=users.get(0);
								nick.setText(user.getNick());
								pass.setText(user.getPass());
							}
						}
				}
			}
		}
	}
	

}
